package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MovementMQService {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送圈子操作相关的消息
     * type 1-发动态， 2-浏览动态， 3-点赞， 4-喜欢， 5-评论， 6-取消点赞， 7-取消喜欢
     *
     * @param publishId
     * @param type
     */
    public void sendMsg(String publishId, Integer type) {

        try {
            Publish publish = publishApi.findById(publishId);

            Map<String, Object> map = new HashMap<>();

            map.put("userId", publish.getUserId());
            map.put("publishId", publishId);
            map.put("pid", publish.getPid());
            map.put("type", type);

            rocketMQTemplate.convertAndSend("tanhua-quanzi", JSON.toJSONString(map));
        } catch (Exception e) {
            log.info("发送失败，错误信息为：" + e);
            log.info("发送信息失败！ publishId = " + publishId + ", type = " + type);
        }
    }

    //发动态
    public void publishMsg(String publishId) {
        this.sendMsg(publishId, 1);
    }

    //浏览动态
    public void queryPublishMsg(String publishId) {
        this.sendMsg(publishId, 2);
    }

    //点赞
    public void likePublishMsg(String publishId) {
        this.sendMsg(publishId, 3);
    }

    //取消点赞
    public void dislikePublishMsg(String publishId) {
        this.sendMsg(publishId, 6);
    }

    //喜欢
    public void LovePublishMsg(String publishId) {
        this.sendMsg(publishId, 4);
    }

    //取消喜欢
    public void disLovePublishMsg(String publishId) {
        this.sendMsg(publishId, 7);
    }

    //评论
    public void commentPublishMsg(String publishId) {
        this.sendMsg(publishId, 5);
    }
}
